$(function() {
	softIds = [];
	getVersionTypes();
	lkjPlatFormId = '';
});
// 初始化右侧版本换装内容
function getVersionTypes() {
	$("#contentSoftBase").empty();
	softIds = [];
	var content = [ {
		ID : "1",
		SYS_NAME : "LKJ软件版本"
	} ];
	var cellContent = [ {
		name : 'DMI单元'
	}, {
		name : '主机单元'
	}, {
		name : '扩展单元'
	} ];
	for (var i = 0; i < content.length; i++) {
		for (var j = 0; j < cellContent.length; j++) {
			str = "";
			str += '<div class="module" id=' + content[i].ID + '><h4>';
			str += cellContent[j].name;
			str += '</h4>';
			str += getSelectValue(getVersionTypesName(content[i].ID), j);
			$("#contentSoftBase").append(str);
		}
	}
	$('.multipleSelect').multiselect({
		enableClickableOptGroups : true,
		enableCollapsibleOptGroups : true,
		includeSelectAllOption : true
	});
	$("#changeBtn").prop("disabled", false);
	//根据后台返回数据显示哪些被选中
	setSelectedFun();
}

//根据后台返回数据显示哪些被选中
function setSelectedFun(){
	console.log(selectTargetData);
	var selectOptionList = JSON.parse(selectTargetData.attributes[1].value);
	if(selectOptionList && selectOptionList.length>0){
		for(var i = 0;i<selectOptionList.length;i++){
			$("#"+selectOptionList[i].id +" select.signSelect option[value='"+selectOptionList[i].fileId+"']").attr("selected",true);
			var postionArray = selectOptionList[i].position.split("");
			for(var j = 0;j<postionArray.length;j++){
				if(postionArray[j] =="1"){
					$("#"+selectOptionList[i].id+"  select.multipleSelect")[0][j].selected = true;
				}
			}
		}
		$(".multipleSelect").multiselect("rebuild");
		$(".multipleSelect").multiselect("refresh");
	}
}
// 根据父节点ID获取当前软件版本下都有哪些类型的软件
function getVersionTypesName(sId) {
	var content = null;
	$.ajax({
		url : "../../../LKJSoftwareVersion/getTypeName",
		type : "post",
		data : {
			"sId" : sId,
			"type" : ""
		},
		dataType : "json",
		async : false,
		success : function(data) {
			if (data.flag == 0) {
				content = data.content;
			} else {
				content = "";
			}
		},
		error : function() {
		}
	});
	// 重置获取的软件数据的顺序，为了排版美观
	resetContent(content);
	return content;
}

// 重置获取的软件数据的顺序，为了排版美观
function resetContent(content) {
	// 把揭示数据添加到主机单元中
	content.splice(17, 0, content[23]);
	// 删除多余的揭示数据
	content.splice(24, 1);
	// 功能扩展和主控位置替换
	content.splice(18, 1, content.splice(21, 1, content[18])[0]);
}
// 将查询到的软件类型进行组装展示各个标签 [{name:'DMI单元'},{name:'主机单元'},{name:'扩展单元'}]
function getSelectValue(content, index) {
	var str = "";
	if (content != "" && content != null) {
		// 3个单元的个数索引
		var start = 0;
		var end = 0;
		switch (index) {
		case 0:
			start = 0;
			end = 6;
			break;
		case 1:
			start = 6;
			end = 18;
			break;
		case 2:
			start = 18;
			end = 24;
			break;
		default:
			break;
		}
		for (var i = start; i < end; i++) {
			var id = content[i].SID;// 软件大类：1，lkj软件，4.asts软件
			var type = content[i].TYPE;// 软件类型
			var softId = id + "_" + type;
			softIds.push(softId);
			str += '<div id=' + softId + '><span class="spans">';
			str += content[i].SYS_NAME;
			str += '</span><select class="signSelect">';
			str += getVersionName(id, type);
		}
	}
	
	return str;
}
// 根据版本类型和软件类型查询下拉文件内容
function getVersionName(id, type) {
	var str = "";
	$.ajax({
		url : "../../../LKJSoftwareVersion/getVersionName",
		type : "post",
		data : {
			"sId" : id,
			"type" : type
		},
		dataType : "json",
		async : false,
		success : function(data) {
			if (data.flag == 0) {
				var content = data.content;
				str = ajaxOption(content);
			}
		},
		error : function() {
		}
	});
	str += '</select>';
	var str1 = "1,3,4,5,6";
	var str2 = "7,8,9,10,11,12,13";
	if (id == 1 && str1.indexOf(type) >= 0) {
		str += "<select class='multipleSelect' multiple='multiple' > <optgroup label='I 端' class='group-1'><option value='1-1'>A机</option>"
				+ "<option value='1-2' >B机</option> </optgroup><optgroup label='II端' class='group-2'>"
				+ "<option value='2-1'>A机</option><option value='2-2'>B机</option> </optgroup></select>";
	} else if (id == 1 && type == 2) {
		str += "<select class='multipleSelect' multiple='multiple'><option value='1'>I 端</option><option value='2'>II端</option></select>";
	} else if (id == 1 && str2.indexOf(type) >= 0) {
		str += "<select class='multipleSelect' multiple='multiple' > <optgroup label='I 系' class='group-1'><option value='1-1'>A模</option>"
				+ "<option value='1-2' >B模</option> </optgroup><optgroup label='II系' class='group-2'>"
				+ "<option value='2-1'>A模</option><option value='2-2'>B模</option> </optgroup></select>";
	} else if (id == 1 && (type == 14)) {
		str += "<select class='multipleSelect' multiple='multiple'><option value='1'>I 系</option><option value='2'>II系</option></select>";
	} else if (id == 1 && type == 16) {
		str += "<select class='multipleSelect' multiple='multiple' >"
				+ " <optgroup label='电源' class='group-1'><option value='1-1'>I 系</option><option value='1-2' >II系</option> </optgroup>"
				+ "<optgroup label='数字' class='group-2'><option value='2-1'>I 系</option><option value='2-2'>II系</option></optgroup>"
				+ "<optgroup label='模拟' class='group-3'><option value='3-1'>I 系</option><option value='3-2'>II系</option> </optgroup>"
				+ "<optgroup label='通信' class='group-4'><option value='4-1'>I 系</option><option value='4-2'>II系</option> </optgroup>"
				+ "<optgroup label='主控' class='group-5'><option value='5-1'>I 系</option><option value='5-2'>II系</option> </optgroup>"
				+ "<optgroup label='检测' class='group-6'><option value='6-1'>I 系</option><option value='6-2'>II系</option> </optgroup>"
				+ "<optgroup label='冷却' class='group-7'><option value='7-1'>I 系</option><option value='7-2'>II系</option> </optgroup>"
				+ "<optgroup label='制动' class='group-8'><option value='8-1'>制动</option> </optgroup>"
				+ "<optgroup label='隔离' class='group-9'><option value='9-1'>隔离</option> </optgroup>"
				+ "</select>";
	} else if (id == 1
			&& (type == 15 || type == 17 || type == 19 || type == 21)) {
		str += "<select class='multipleSelect' multiple='multiple'><option value='1'>A机</option><option value='2'>B机</option></select>";
	} else {
		str += "<div style='width:100px'></div>";
	}
	return str += '</div>';
}
// 将查询到的下拉内容组合放到对应的位置
function ajaxOption(content) {
	var str = "<option value=''></option>";
	if (content != "" && content != null) {
		for (var i = 0; i < content.length; i++) {
			var name = content[i].VERSION_NAME;
			var name1 = "";
			var name2 = "";// 将文件名以逗号分隔加入版本号信息
			var nameTemp = name.split(".");
			if (nameTemp.length == 2) {
				name1 = nameTemp[0];
				name2 = "." + nameTemp[1];
			} else {
				name1 = nameTemp[0];
			}
			str += "<option value=" + content[i].VERSION_ID + ">" + name1 + "_"
					+ content[i].VERSION_NUMBER + name2 + "("
					+ content[i].VERSION_DETAIL + ")" + "</option>";
		};
	}
	return str;
}
